#include "music_play.h"
#include "game_logic.h"


struct game_music_info_t {
	const char * scene_name;
	const NSString* const music_name;
};
const game_music_info_t GAME_SCENE_MUSIC[] = {
{"matou", @"matou.caf"},
{"shamo", @"shamo.caf"},
{"pingyuan", @"muchang.caf"},
{"senlin", @"conglin.caf"}
};

scoped_object_c_ptr<SoundPlayerWrapper> g_soundPlayer;

void loadMenuMusic()
{
	if (NULL == g_soundPlayer.get()) g_soundPlayer.reset([[SoundPlayerWrapper alloc] init]);
	[g_soundPlayer.get() StopAll];
	[g_soundPlayer.get() Add:kSoundKeyPress];
	[g_soundPlayer.get() Add:kSoundMainMenu bQueue:true];
	playMusic(kSoundMainMenu, true);
}

void loadGameMusic()
{
	if (NULL == g_soundPlayer.get()) g_soundPlayer.reset([[SoundPlayerWrapper alloc] init]);
	[g_soundPlayer.get() StopAll];
	[g_soundPlayer.get() Add:kSoundKeyPress];
	[g_soundPlayer.get() Add:kSoundExplode];
	[g_soundPlayer.get() Add:kSoundBroken];
	const std::string &bgImage = game_logic::getCurLevelBgImag();
	BOOST_FOREACH(const game_music_info_t& info, GAME_SCENE_MUSIC) {
		if (std::string::npos != bgImage.find(info.scene_name)) {
			[g_soundPlayer.get() Add:info.music_name bQueue:true];
			playMusic(info.music_name, true);
		}
		else {
			[g_soundPlayer.get() Remove:info.music_name];
		}
	}
}

void playMusic(const NSString *filename, bool loop)
{
	ASSERT(NULL != g_soundPlayer.get());
	if (game_logic::isMusicPlay()) [g_soundPlayer.get() Play:filename Looping:loop];
}
void playStop()
{
	[g_soundPlayer.get() StopAll];
}

